package com.scitc.pleasurevideo.woview;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.base.ETActivity;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.data.UserDataMan;
import com.scitc.pleasurevideo.datasql.VideoSourceSqlData;
import com.scitc.pleasurevideo.dialog.DialogSettingFlowDownLoadSizeEveryDay;
import com.scitc.pleasurevideo.dialog.DialogSettingRadio;
import com.scitc.pleasurevideo.helper.ForwardHelper;
import com.scitc.pleasurevideo.util.ConstUtil;
import com.scitc.pleasurevideo.x5webview.MyApplication;

/**
 * 我的界面-设置按钮-设置界面
 * @author contradiction
 * @time 2020-4-4
 */
public class SettingActivity extends ETActivity {

    private RelativeLayout rlIsFlowPlayBtn;
    private ToggleButton togg1;
    private RelativeLayout rlIsFlowDownloadBtn;
    private ToggleButton togg2;
    private RelativeLayout rlIsHistoryBtn;
    private ToggleButton togg3;
    private RelativeLayout rlIsHistoryDifferentSourceBtn;
    private TextView tvIsHistoryDifferentSourceText;
    private RelativeLayout rlVideoSourceBtn;
    private TextView tvVideoSourceText;
    private RelativeLayout rlIsClearCachingBtn;
    private RelativeLayout rlAboutBtn;
    private RelativeLayout rlFlowDownloadSizeEveryDayBtn;
    private TextView tvFlowDownloadSizeEveryDayText;
    private LinearLayout llBigBox;
    private RelativeLayout rlUserVideoSourceBtn;
    private TextView tvExitUserBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        onclick();
        initData();
        initDownloadUI();
    }


    /**
     * 初始化数据
     */
    @SuppressLint("DefaultLocale")
    private void initData() {
        //加载本地缓存的----允许存储播放历史记录--权限
        togg3.setChecked(ETMan.getPref(mInstance,ConstUtil.IS_SAVE_PLAY_HISTORY_LIST,true));
        //加载本地缓存的----允许2G/3G/4G播放--权限
        togg1.setChecked(ETMan.getPref(mInstance,ConstUtil.IS_FLOW_PLAY_VIDEO,false));
        //加载本地缓存的----允许2G/3G/4G下载--权限
        togg2.setChecked(ETMan.getPref(mInstance,ConstUtil.IS_FLOW_DOWNLOAD_VIDEO,false));
        //加载本地缓存的----设置每日流量下载上限---大小（M）
        tvFlowDownloadSizeEveryDayText.setText(String.format("%dM",ETMan.getPref(mInstance,ConstUtil.FLOW_DOWNLOAD_SIZE_EVERY_DAY,ConstUtil.FLOW_DOWNLOAD_SIZE_DEFAULT_SIZE)));
        //加载本地缓存的----历史存储方式
        tvIsHistoryDifferentSourceText.setText(ConstUtil.HISTORY_TYPE_ARRAY[ETMan.getPref(mInstance, ConstUtil.IS_HISTORY_DIFFERENT_SOURCE, ConstUtil.HISTORY_TYPE_DEFAULT)]);
        //加载本地缓存的----影视来源个数和选中的个数
        tvVideoSourceText.setText(VideoSourceSqlData.getDbDataManager().getSelectVideoSourceNumber());
        //当用户登录时显示的控件
        if(UserDataMan.init().isUserLogin()){
            rlUserVideoSourceBtn.setVisibility(View.VISIBLE);
            tvExitUserBtn.setVisibility(View.VISIBLE);
        }else {
            rlUserVideoSourceBtn.setVisibility(View.GONE);
            tvExitUserBtn.setVisibility(View.GONE);
        }

    }

    @SuppressLint("DefaultLocale")
    private void onclick() {
        //允许存储播放历史记录
        rlIsHistoryBtn.setOnClickListener(v -> {
            if (togg3.isChecked()) {
                Toast.makeText(mInstance, "不允许存储历史记录", Toast.LENGTH_SHORT).show();
                togg3.setChecked(false);
                ETMan.savePref(mInstance,ConstUtil.IS_SAVE_PLAY_HISTORY_LIST,false);
            } else {
                Toast.makeText(mInstance, "允许存储历史记录", Toast.LENGTH_SHORT).show();
                togg3.setChecked(true);
                ETMan.savePref(mInstance,ConstUtil.IS_SAVE_PLAY_HISTORY_LIST,true);
            }
        });

        //允许2G/3G/4G播放
        rlIsFlowPlayBtn.setOnClickListener(v -> {
            if (togg1.isChecked()) {
                Toast.makeText(mInstance, "关闭了流量播放", Toast.LENGTH_SHORT).show();
                togg1.setChecked(false);
                ETMan.savePref(mInstance,ConstUtil.IS_FLOW_PLAY_VIDEO,false);
            } else {
                Toast.makeText(mInstance, "开启了流量播放", Toast.LENGTH_SHORT).show();
                togg1.setChecked(true);
                ETMan.savePref(mInstance,ConstUtil.IS_FLOW_PLAY_VIDEO,true);
            }
        });

        //允许2G/3G/4G下载
        rlIsFlowDownloadBtn.setOnClickListener(v -> {
            if (togg2.isChecked()) {
                Toast.makeText(mInstance, "关闭了流量下载", Toast.LENGTH_SHORT).show();
                togg2.setChecked(false);
                ETMan.savePref(mInstance,ConstUtil.IS_FLOW_DOWNLOAD_VIDEO,false);
            } else {
                Toast.makeText(mInstance, "开启了流量下载", Toast.LENGTH_SHORT).show();
                togg2.setChecked(true);
                ETMan.savePref(mInstance,ConstUtil.IS_FLOW_DOWNLOAD_VIDEO,true);
            }
        });

        //设置每日流量下载上限---tvFlowDownloadSizeEveryDayText
        rlFlowDownloadSizeEveryDayBtn.setOnClickListener(v ->
            new DialogSettingFlowDownLoadSizeEveryDay(llBigBox, mInstance, everyDaySize ->
              tvFlowDownloadSizeEveryDayText.setText(String.format("%dM",everyDaySize))
            )
        );

        //不同来源相同影视存历史记录---tvIsHistoryDifferentSourceText
        rlIsHistoryDifferentSourceBtn.setOnClickListener(v ->
                new DialogSettingRadio(llBigBox, mInstance,"历史存储方式", ConstUtil.HISTORY_TYPE_ARRAY, ETMan.getPref(mInstance, ConstUtil.IS_HISTORY_DIFFERENT_SOURCE, ConstUtil.HISTORY_TYPE_DEFAULT), position ->{
                        ETMan.savePref(mInstance, ConstUtil.IS_HISTORY_DIFFERENT_SOURCE,position);
                        tvIsHistoryDifferentSourceText.setText(ConstUtil.HISTORY_TYPE_ARRAY[position]);
                    }
                )
        );

        //设置影视来源---tvVideoSourceText
        rlVideoSourceBtn.setOnClickListener(v -> ForwardHelper.toSettingVideoSourceListActivity(mInstance));
        //设置影视来源的回调
        SettingVideoSourceListActivity.setOnVideoSourceNumberStr(selectStr -> tvVideoSourceText.setText(selectStr));
        //管理员设置影视来源
        rlUserVideoSourceBtn.setOnClickListener(view -> ForwardHelper.toUserSettingVideoSourceActivity(mInstance));
        //清空缓存
        rlIsClearCachingBtn.setOnClickListener(v -> Toast.makeText(mInstance, "暂未开发", Toast.LENGTH_SHORT).show());
        //关于
        rlAboutBtn.setOnClickListener(v -> Toast.makeText(mInstance, "暂未开发", Toast.LENGTH_SHORT).show());
        //退出登录
        tvExitUserBtn.setOnClickListener(v -> {
            UserDataMan.init().exitUser();
            finish();
        });

    }

    private void initView() {
        llBigBox = (LinearLayout) findViewById(R.id.llBigBox);
        rlIsFlowPlayBtn = (RelativeLayout) findViewById(R.id.rlIsFlowPlayBtn);
        togg1 = (ToggleButton) findViewById(R.id.togg1);
        rlIsFlowDownloadBtn = (RelativeLayout) findViewById(R.id.rlIsFlowDownloadBtn);
        togg2 = (ToggleButton) findViewById(R.id.togg2);
        rlIsHistoryBtn = (RelativeLayout) findViewById(R.id.rlIsHistoryBtn);
        togg3 = (ToggleButton) findViewById(R.id.togg3);
        rlIsHistoryDifferentSourceBtn = (RelativeLayout) findViewById(R.id.rlIsHistoryDifferentSourceBtn);
        tvIsHistoryDifferentSourceText = (TextView) findViewById(R.id.tvIsHistoryDifferentSourceText);
        rlVideoSourceBtn = (RelativeLayout) findViewById(R.id.rlVideoSourceBtn);
        rlUserVideoSourceBtn = (RelativeLayout) findViewById(R.id.rlUserVideoSourceBtn);
        tvVideoSourceText = (TextView) findViewById(R.id.tvVideoSourceText);
        rlIsClearCachingBtn = (RelativeLayout) findViewById(R.id.rlIsClearCachingBtn);
        rlAboutBtn = (RelativeLayout) findViewById(R.id.rlAboutBtn);
        rlFlowDownloadSizeEveryDayBtn = (RelativeLayout) findViewById(R.id.rlFlowDownloadSizeEveryDayBtn);
        tvFlowDownloadSizeEveryDayText = (TextView) findViewById(R.id.tvFlowDownloadSizeEveryDayText);
        tvExitUserBtn = (TextView) findViewById(R.id.tvExitUserBtn);
    }

    /**
     * 判断是否显示下载ui
     */
    private void initDownloadUI() {
        if(MyApplication.isShowDownloadUi){
            rlIsFlowDownloadBtn.setVisibility(View.VISIBLE);
            rlFlowDownloadSizeEveryDayBtn.setVisibility(View.VISIBLE);
        }else {
            rlIsFlowDownloadBtn.setVisibility(View.GONE);
            rlFlowDownloadSizeEveryDayBtn.setVisibility(View.GONE);
        }
    }
}
